I have a comments attribute containing RTF which I am trying to convert to text to be pasted into an excel spreadsheet. The RTF causes problems for Excel, so I convert the DOORS RTF to standard RTF, which leaves residual tags "\par" and \tab". I then replace the tags with spaces for readability, but it doesn't work. My code is as follows (derived from a snipped posted previously on this forum) the code works for a string such as "par" but does not find "\\par". I tried using a regular expression to match \par but that gave me a syntax error for the replace function. What am I doing wrong? Thanks, Ken.
//Function to strip RTF tags out of the buffer text
void combine ( Buffer b, string s )
{
Buffer tmp = create
tmp = s
combine ( b, tmp, 0 )
delete tmp
}
Buffer replace ( Buffer b, string toReplace, string newVal )
{
Buffer rez = create
int pivot = 0
int iposition = contains ( b, toReplace, pivot )
while ( iposition != -1 )
{
combine ( rez, b, pivot, iposition-1 )
combine ( rez, newVal )
pivot = iposition + length toReplace
iposition = contains ( b, toReplace, pivot )
}
combine ( rez, b, pivot )
return rez
}
// later...
for o in m do
{
text_Comments = o."Comments"
text_Comments=exportRTFString(text_Comments)
Buffer b1 = create (5000)
b1=text_Comments
Buffer newBuff = replace( b1, "\\par", " " )
list = list "\t" ObjId "\t" (tempStringOf newBuff) "\n"
delete(b1)
delete newBuff
}
mcnairk - Tue Jun 02 14:01:58 EDT 2015 |
Re: Replacing a literal string in a buffer Consider the following commands to retrieve info from a "Comments" Text attribute:
I think you need that last one then you can abandon dealing with rich-text codes. Then replace all '\n' and '\t' characters with a space. I think that looks like this:
-Louie |
Re: Replacing a literal string in a buffer llandale - Tue Jun 02 20:08:48 EDT 2015 Consider the following commands to retrieve info from a "Comments" Text attribute:
I think you need that last one then you can abandon dealing with rich-text codes. Then replace all '\n' and '\t' characters with a space. I think that looks like this:
-Louie Thanks Louie, I'll give this a try and let you know...Ken. |
Re: Replacing a literal string in a buffer mcnairk - Thu Jun 04 08:37:32 EDT 2015 Thanks Louie, I'll give this a try and let you know...Ken. By the way: If you want to match a \par inside a regular expression you need to use to escape the backslashes once for the regular expression \\par ... And if you want to put this in a string literal you have to escape each backslash again for DXL: string s = "\\\\par" print s // prints "\\par" regexp s // matches "\par" Additionally you should consider a check, if an uneven number of backslashes were matched (which means, that you are not looking at an literal "\par" inside the RTF ... Regards, Mathias |
Re: Replacing a literal string in a buffer mcnairk - Thu Jun 04 08:37:32 EDT 2015 Thanks Louie, I'll give this a try and let you know...Ken. Two problems: - E- DXL: <Line:194> incorrect arguments for (+=) if I try to assign the attribute directly into the buffer, which goes way if I do the following: text_Comments = o."Comments" bResults += text_Comments - This is a lot simpler than my original code, but gives the same output containing \par and \tab within the text.I have to try Matthias' suggestion to get rid of this... |
Re: Replacing a literal string in a buffer Mathias Mamsch - Thu Jun 04 09:49:58 EDT 2015 By the way: If you want to match a \par inside a regular expression you need to use to escape the backslashes once for the regular expression \\par ... And if you want to put this in a string literal you have to escape each backslash again for DXL: string s = "\\\\par" print s // prints "\\par" regexp s // matches "\par" Additionally you should consider a check, if an uneven number of backslashes were matched (which means, that you are not looking at an literal "\par" inside the RTF ... Regards, Mathias Thanks to both of you I have now resolved this problem. I♥DXL! |